package com.shujia.jichu

object Demo15Set {
  def main(args: Array[String]): Unit = {

    /**
     * Set集合：元素无序且唯一
     * 除了排序以外，List集合有的函数，Set集合都有
     */
    //创建一个Set集合
    val set1: Set[Int] = Set(88, 66, 22, 99, 11, 33, 110, 11, 22, 44, 22, 55, 11, 33)
    println(set1)
    set1.foreach(println)
    println(set1.mkString("|"))
    println("=" * 100)

    //交集，并集，差集
    val set2: Set[Int] = Set(1, 2, 3, 4, 5)
    val set3: Set[Int] = Set(3, 4, 5, 6, 7)

    //求交集
    println(set2.&(set3))
    println(set2 & set3)
    println(set2.intersect(set3))
    //求并集
    println(set2.|(set3))
    println(set2 | set3)
    //求差集
    println(set2.&~(set3))
    println(set2 &~ set3)

    /**
     * 集合之间的转换
     * List -> Set
     *
     */
    val list1: List[Int] = List(88, 66, 22, 99, 11, 33, 110, 11, 22, 44, 22, 55, 11, 33)
    println(s"list1: ${list1}")
    //List转Set集合
    val set4: Set[Int] = list1.toSet
    println(s"set4: ${set4}")
    //Set转List集合
    val list2: List[Int] = set4.toList
    println(s"list2: ${list2}")


  }
}
